'use strict'
const Controller = require('egg').Controller // test

class HomeController extends Controller {
  async index() {
    // this.ctx.body = "api接口"
    let result = await this.app.mysql.get("type",{})
    console.log(result)
    this.ctx.body = result
  }
  async getArticleList(){//获取文章列表
    let sql = 'SELECT article.id as id,'+
              'article.title as title,'+
              'article.introduce as introduce,'+
              'article.addTime as addTime,'+
              'article.view_count as view_count,'+
              '.type.typeName as typeName '+
              'FROM article LEFT JOIN type ON article.type_id = type.Id'
    const results = await this.app.mysql.query(sql)
    // console.log(results);
    this.ctx.body={
      data:results
    }
  }

  async getArticleById(){//根据id获取文章内容
    let id = this.ctx.params.id
    let sql = 'SELECT article.id as id,'+
              'article.title as title,'+
              'article.introduce as introduce,'+
              'article.article_content as article_content,'+
              "FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime,"+
              'article.view_count as view_count ,'+
              'type.typeName as typeName ,'+
              'type.id as typeId '+
              'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
              'WHERE article.id='+id

    const results = await this.app.mysql.query(sql)
    this.ctx.body = {
      data:results
    }
  }

  async getTypeInfo(){//获取类型信息 ---导航菜单
    const result = await this.app.mysql.select('type')
    console.log("-------------->",result)
    this.ctx.body = {data:result}
  }
  async getListById(){//根据类别id获取文章列表
    let id = this.ctx.params.id
    let sql = 'SELECT article.id as id,'+
        'article.title as title,'+
        'article.introduce as introduce,'+
        'FROM_UNIXTIME(article.addTime,"%Y-%m-%d %H:%i:%s") as addTime,'+
        'article.view_count as view_count,'+
        'type.typeName as typeName '+
        'FROM article LEFT JOIN type on article.type_id = type.Id '+
        'WHERE type_id='+id
      const result = await this.app.mysql.query(sql)
      this.ctx.body={data:result}
  }

}
module.exports = HomeController
